package y24.m3;

import java.util.Stack;

public class Test11 {
    static class Solution {
        public static int evalRPN(String[] tokens) {
            Stack<Integer> stack = new Stack<>();
            for(String s : tokens){
                if(isOperation(s)){
                    int num = stack.pop();
                    if(s.equals("+")){
                        stack.push(stack.pop() + num);
                    }else if(s.equals("-")){
                        stack.push(stack.pop() - num);
                    }else if(s.equals("*")){
                        stack.push(stack.pop() * num);
                    }else if(s.equals("/")){
                        stack.push(stack.pop() / num);
                    }
                }else{
                    stack.push(Integer.parseInt(s));
                }
            }
            return stack.pop();
        }
        private static boolean isOperation(String s){
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                return true;
            }
            return false;
        }
    }
    public static void main(String[] args) {
        String[] s = {"2","1","+","3","*"};
        System.out.println(Solution.evalRPN(s));

    }
}
